<?php
/**
 * ============================================================================
 * Copyright (c) 2015-2019 贵州大师兄信息技术有限公司 All rights reserved.
 * siteַ: http://www.dsxcms.com
 * ============================================================================
 * @author:     David Song<songdewei@163.com>
 * @version:    v1.0.0
 * ---------------------------------------------
 * Date: 2019-06-02
 * Time: 10:28
 */

namespace App\Library\User;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

trait SubscribeManagers
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        $subscribe_uid = $request->input('subscribe_uid', 0);
        $subscribe = Auth::user()->subscribes()->where('subscribe_uid', $subscribe_uid)->first();
        if (!$subscribe) {
            $subscribe = Auth::user()->subscribes()->create(['subscribe_uid' => $subscribe_uid]);
        }
        return ajaxReturn(['subscribe' => $subscribe]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request){
        $subscribe_uid = $request->input('subscribe_uid', 0);
        Auth::user()->subscribes()->where('subscribe_uid', $subscribe_uid)->delete();
        return ajaxReturn();
    }

    /**
     * @param Request $request
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
     */
    protected function batchgetSubscribes(Request $request){
        $offset = $request->input('offset',0);
        $count = $request->input('count',20);
        $items = Auth::user()->subscribes()->with('subscribeUser')->offset($offset)->take($count)->get();
        return $items;
    }

    /**
     * @param Request $request
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
     */
    protected function batchgetFans(Request $request){
        $offset = $request->input('offset',0);
        $count = $request->input('count',20);
        $items = Auth::user()->fans()->with('user')->offset($offset)->take($count)->get();
        return $items;
    }
}
